package com.education.basic.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.education.basic.dao.ScoreDAO;
import org.springframework.stereotype.Service;

import com.education.basic.dao.TeachInfoDAO;
import com.education.basic.entity.TeachInfo;
import com.education.basic.resource.Courses;
import com.education.basic.service.TeachInfoService;
import com.yunshangwoniu.core.baseService.PageService;
import com.yunshangwoniu.core.page.Page;
import com.yunshangwoniu.core.utils.DateUtils;
import com.yunshangwoniu.core.utils.Logger;
import com.yunshangwoniu.core.utils.RequestUtils;
import org.springframework.transaction.annotation.Transactional;

@Service("teachInfoService")
public class TeachInfoServiceImpl extends BaseServiceImpl<TeachInfo> implements TeachInfoService
{
	@Resource
	private TeachInfoDAO teachInfoDAO;
	@Resource
	private PageService<TeachInfo> pageService;
    @Resource
    private ScoreDAO scoreDAO;

	@Override
    @Transactional
	public Page<TeachInfo> queryTeachInfoWhenExsit(int currentPageNum, int pageSize, Map<String, Object> params,HttpServletRequest request)
	{
		Page<TeachInfo> page = new Page<TeachInfo>(currentPageNum, pageSize,RequestUtils.getPageUrl(request));
		page.setParams(params);
		page = teachInfoDAO.query(page, TeachInfo.class);
		pageService.htmlBuilder(page, "default");
		try
		{
			Logger.debug(this.getClass(), String.format("get assigned teacher courses size is %s ", page.getRecords().size()));
		}catch(Exception e)
		{
			Logger.debug(this.getClass(), String.format("get assigned teacher courses size is %s ", 0));
		}
		return page;
	}

	@Override
    @Transactional
	public List<Courses> queryTeachInfoWhenNoExsit()
	{
		List<TeachInfo> infos = teachInfoDAO.queryAll();
		List<Courses> courses = Courses.queryCourseByMajorCodeAndCoursesType(null, null);
		Logger.debug(this.getClass(), String.format("get all courses size is %s ", courses.size()));
		List<String> codes =  new ArrayList<String>();
        List<Courses> returnList = new ArrayList<Courses>();
		for(TeachInfo info : infos)
		{
			codes.add(info.getCourseCode());
		}
		for(Courses c : courses)
		{
			if(!codes.contains(c.getCode()))
			{
                returnList.add(c);
			}
		}
		Logger.debug(this.getClass(), String.format("get don't assigned teacher courses size is %s ", returnList.size()));
 		return returnList;
	}

	@Override
    @Transactional
	public TeachInfo assigne(TeachInfo info)
	{
		info.setCreateTime(System.currentTimeMillis());
		info.setCreateTimeStr(DateUtils.getCurrentTime());
		info.setLastAccessStr(DateUtils.getCurrentTime());
		info.setLastAccess(System.currentTimeMillis());
		teachInfoDAO.save(info);
		Logger.debug(this.getClass(), String.format("assigne teacher is success when [teacherNum:%s;courseCode:%s;courseName:%s]", info.getEmpno(),info.getCourseCode(),info.getCourseName()));
        scoreDAO.updateScores(info.getCourseCode(),info.getTeacherName(),info.getEmpno());
		return info;
	}

	@Override
    @Transactional
	public Page<TeachInfo> findByCourseName(String courseName) 
	{
		Page<TeachInfo> page = new Page<TeachInfo>(0, 0,null);
		List<TeachInfo> records = new ArrayList<TeachInfo>();
		TeachInfo info = teachInfoDAO.getByCourseName(courseName);
		if(info!=null)
		{
			records.add(info);
			page.setRecords(records);
		}
		pageService.htmlBuilder(page, "default");
		return page;
	}

    @Override
    @Transactional
    public TeachInfo findTeachInfoByCourseCode(String courseCode) {
        return teachInfoDAO.getByCourseCode(courseCode);
    }

}
